var app = getApp();
var __ctxPath = app.__ctxPath;

Page({
  data: {
    // 搜索框状态
    inputShowed: false,
    // 搜索框值
    inputVal: "",
    //搜索渲染推荐数据
    list: [
      // { name: "帝霸" },
      // { name: "遮天" },
      // { name: "道界天下" },
      // { name: "菜鸟" },
      // { name: "先飞" }
    ]
  },
  // 显示搜索框
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  // 隐藏搜索框样式
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  // 清除搜索框值
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  // 获取搜索框值
  inputTyping: function (e) {
    let val = e.detail.value;

    var that = this;
    wx.request({
      url: __ctxPath + '/outsideInterface/YxcOutsideInterface.do?method=listYxcGoods',
      data: { 'name': val },
      success: function (res) {
        //console.log(res.data);
        if ("0" != res.data.code) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000//持续的时间
          });
        } else {
          // let list = [
          //   { lid: "1", name: "帝霸" },
          //   { lid: "2", name: "遮天" },
          //   { lid: "3", name: "道界天下" }
          // ]

          that.setData({
            inputVal: val
            , list: res.data.data
          });
        }
      }
    });
  },
  // 获取选中推荐列表中的值
  btn_name: function (res) {
    let vo = res.currentTarget.dataset;
    this.clearInput();
    this.hideInput();
    wx.navigateTo({ url: '/pages/yxc/costPrice/costPrice?vo=' + JSON.stringify(vo) });
  },
  /**
   * 打开相机扫一扫，条形码
   */
  scan: function () {
    var that = this;
    wx.scanCode({
      onlyFromCamera: false, // 只允许从相机扫码
      success(res) {
        if ("scanCode:ok" != res.errMsg) {
          alert("扫码错误");
          return;
        }

        wx.showLoading({ title: '数据加载中' });
        setTimeout(() => {
          //响应可能超时，为了提高用户体验，所以取消加载框
          wx.hideLoading();
        }, 5 * 1000);
        var barcode = res.result;
        try {
          wx.request({
            url: __ctxPath + '/outsideInterface/YxcOutsideInterface.do?method=getYxcGoods',
            data: { barcode: barcode },
            success: function (res) {
              var yxcGood = res.data.data;
              if (!yxcGood){
                wx.showToast({
                  title: '暂未收录该商品',
                  icon: 'none',
                  duration: 2000//持续的时间
                });
              }else{
                yxcGood.name = `${yxcGood.name}(${yxcGood.standard})`;
                wx.navigateTo({ url: '/pages/yxc/costPrice/costPrice?vo=' + JSON.stringify(yxcGood) });
              }
            }
          });
        } catch (err) {
          //响应超时
          wx.hideLoading();
          that.setData({
            vo: { name: "暂未收录该商品" }
          })
        }
      }
    })
  }
  // 结束
});